package app.view.component.movie 
{
	import app.facade.App;
	import app.view.component.HumanRun;
	import com.greensock.TweenLite;
	import com.temple.core.CoreMovieClip;
	import com.Utils;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	import flash.sensors.Accelerometer;
	import flash.events.AccelerometerEvent;
	/**
	 * ...
	 * @author PHANCUONG87
	 */
	[Embed(source="../../../../../lib/ui.swf", symbol="Schwanker")]
	public class Schwanker extends MovieClip
	{
		public var Mund:MovieClip;
		public var mcHandRight:MovieClip;
		public var mcHandLeft:MovieClip;
		public var mcFoot:MovieClip;
		public var mcHead:MovieClip;
		public var mcShadow:MovieClip;
		
		public var rootTime:Number = 0;
		public var textfield:TextField; 
		public var tx:Number = 0;
		public var ty:Number; 
		
		public function Schwanker() 
		{
			addEventListener(Event.ADDED_TO_STAGE, onInital);
		}
		public function onInital(evt:Event):void {
			this.gotoAndStop(1);
			initHuman();
			var acc :Accelerometer = new Accelerometer();
			acc.addEventListener(AccelerometerEvent.UPDATE, update);
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			//TweenLite.delayedCall(10, level1);
			
		}
		
		public function run():void {
			App.Besauffaktor = 10;
			App.randomizerKorrektur = 0;
			App.randomizer = 0;
			App.schwankfaktor = 1;
			App.zeit = 0;
			App.rotationSchwanker = 0;
			App.stehen = 0;	
			
			App.stehen = 1;
			App.rotationSchwanker = 0;
			rotationHandRight();
			rotationHandLeft();
			rotationFoot();
			rotationShadow();
			rotationHead();
			rotationMund();
			this.gotoAndStop(1);
			rootTime = getTimer();
			
			level1();
		}
		
		
		public function update(e:AccelerometerEvent):void {
			tx = e.accelerationX *100;
			ty = e.accelerationY * 100;
		}
		
		public function initHuman():void {
			mcHandRight.gotoAndStop(1);
			mcHandLeft.gotoAndStop(1);
		}
		
		public function getPosAccele():Number {			
			//var r:Number = App.deviceSize.width / 180;
			var r:Number = 800 / 180;
			var a:Number = tx * r;
			//var result:Number = App.deviceSize.width / 2 - a;
			var result:Number = 960 / 2 - a;
			return result;
		}
		
		public function onEnterFrame(evt:Event):void 
		{
 			if (App.stehen == 1 && getTimer() - rootTime > 1000) {
				if (App.randomizerKorrektur < App.randomizer){
					App.randomizerKorrektur = App.randomizerKorrektur + 0.100000;
				}
				if (App.randomizerKorrektur > App.randomizer){
					App.randomizerKorrektur = App.randomizerKorrektur - 0.100000;
				}
				if (App.MODE == 1){
					App.Faktor = getPosAccele() / 48 - 10 + this.rotation / App.Besauffaktor;
				}else {
					App.Faktor = stage.mouseX / 48 - 10 + this.rotation / App.Besauffaktor;
				}
				
				
				App.rotationSchwanker = this.rotation + App.Faktor + App.randomizerKorrektur * App.schwankfaktor;
				this.rotation = App.rotationSchwanker;
							
				//App.PTest =App.Faktor
			
				
				
				if (App.rotationSchwanker > 50) {
					(this.parent as HumanRun).downRight();
					this.rotation = 0;
					App.stehen = 0;
					TweenLite.killDelayedCallsTo(level1);
					TweenLite.killDelayedCallsTo(level2);
					TweenLite.killDelayedCallsTo(level3);
				} 
				if (App.rotationSchwanker < -50) {
					(this.parent as HumanRun).downLeft();
					this.gotoAndStop(2);
					this.rotation = 0;
					App.stehen = 0;
					TweenLite.killDelayedCallsTo(level1);
					TweenLite.killDelayedCallsTo(level2);
					TweenLite.killDelayedCallsTo(level3);
				} 
			}
			
			rotationHandRight();
			rotationHandLeft();
			rotationFoot();
			rotationShadow();
			rotationHead();
			rotationMund();
		}
		
		public function rotationHandRight():void {
			if(mcHandRight) mcHandRight.rotation = App.rotationSchwanker * 2;
		}
		
		public function rotationHandLeft():void {
			//mcHandRight = App.rotationSchwanker * 2;
		}
		
		public function rotationFoot():void {
			if(mcFoot) mcFoot.rotation = App.rotationSchwanker / 2 * -1;
		}
		
		public function rotationShadow():void {
			if(mcHead) mcHead.rotation = App.rotationSchwanker * -1;
		}
		
		public function rotationHead():void {
			if(mcShadow) mcShadow.rotation = App.rotationSchwanker * -1;
		}

		public function rotationMund():void {
			if(Mund) Mund.rotation = App.rotationSchwanker * -1;
		}
		
		public function level1():void {
			if (App.stehen == 1){
				App.zeit = App.zeit + 1;
			} // end if

			TweenLite.delayedCall(300, level2)
		}
		
		public function level2():void {
			App.randomizer = Utils.getRand(2, 5);
			TweenLite.delayedCall(500, level3)
		}
		
		
		public function level3():void 
		{
			App.Besauffaktor = App.Besauffaktor - 0.500000;
			if (App.Besauffaktor < 3)
			{
				App.Besauffaktor = 3;
				App.schwankfaktor = 0.250000;
			} // end if
		}
		
	}

}